package net.lzzy.chapter512;

import java.io.*;
import java.net.Socket;

import javafx.application.Application;
import javafx.geometry.HPos;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.*;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
public class Client_BMI extends Application {
    DataOutputStream toServer = null;
    DataInputStream fromServr = null;
    @Override
    public void start(Stage primaryStage) {
        BorderPane root = new BorderPane();
        Scene scene = new Scene(root, 400, 200, Color.WHITE);
        TextArea ta = new TextArea();
        GridPane gridpane = new GridPane();
        gridpane.setPadding(new Insets(5));
        gridpane.setHgap(5);
        gridpane.setVgap(5);
        ColumnConstraints column1 = new ColumnConstraints(50);
        ColumnConstraints column2 = new ColumnConstraints(50, 150, 300);
        column2.setHgrow(Priority.ALWAYS);
        gridpane.getColumnConstraints().addAll(column1, column2);


        Label fNameLbl = new Label("身高：");
        TextField height = new TextField();


        Label lNameLbl = new Label("体重：");
        TextField weight = new TextField();


        Button saveButt = new Button("提交");

        saveButt.setOnAction(e -> {
            try {
                double radius_height = Double.parseDouble(height.getText().trim());
                double radius_weight = Double.parseDouble(weight.getText().trim());


                toServer.writeDouble(radius_height);
                toServer.writeDouble(radius_weight);
                toServer.flush();

                double area = fromServr.readDouble();

                ta.appendText("身高:"+ radius_height+"\n");
                ta.appendText("体重:" + radius_weight + "\n");
                ta.appendText("BMI值："+ area + '\n');
            } catch (IOException ex) {
                System.out.println(ex);
            }
        });

        try {
            Socket socket1 = new Socket("localhost", 8000);

            fromServr = new DataInputStream(socket1.getInputStream());

            toServer = new DataOutputStream(socket1.getOutputStream());

        } catch (IOException ex) {
            ta.appendText(ex.toString() + '\n');
        }


        // 名字的标签
        GridPane.setHalignment(fNameLbl, HPos.RIGHT);
        gridpane.add(fNameLbl, 0, 0);

        // 姓名标签
        GridPane.setHalignment(lNameLbl, HPos.RIGHT);
        gridpane.add(lNameLbl, 0, 1);

        // 输入框
        GridPane.setHalignment(height, HPos.LEFT);
        gridpane.add(height, 1, 0);

        GridPane.setHalignment(weight, HPos.LEFT);
        gridpane.add(weight, 1, 1);

        GridPane.setHalignment(saveButt, HPos.RIGHT);
        gridpane.add(saveButt, 1, 2);

        root.setCenter(gridpane);
        root.setBottom(saveButt);
        primaryStage.setScene(scene);
        primaryStage.show();

    }
    public static void main(String[] args){
        launch(args);
    }
}
